package com.sl.proxy.server.controller;

import com.sl.proxy.server.base.page.PageInfo;
import com.sl.proxy.server.base.page.PageQuery;
import com.sl.proxy.server.controller.req.system.*;
import com.sl.proxy.server.controller.res.system.*;
import com.sl.proxy.server.service.PortMappingService;
import com.sl.proxy.server.service.SecurityGroupService;
import com.sl.proxy.server.util.ParamCheckUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/security")
@RequiredArgsConstructor
public class SecurityController {

    private final SecurityGroupService groupService;

    private final PortMappingService portMappingService;

    /**
     * 获取当前用户权限下的安全组
     */
    @GetMapping("/group/page")
    public PageInfo<SecurityGroupListRes> groupPage(PageQuery pageQuery, SecurityGroupListReq req) {
        ParamCheckUtil.checkNotNull(pageQuery, "pageQuery");

        return groupService.groupPage(pageQuery, req);
    }

    @GetMapping("/group/list")
    public List<SecurityGroupListRes> groupList() {
        return groupService.groupList();
    }

    @GetMapping("/group/detail")
    public SecurityGroupDetailRes groupDetail(SecurityGroupDetailReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");

        return groupService.groupDetail(req);
    }

    @PostMapping("/group/create")
    public void createGroup(@RequestBody SecurityGroupCreateReq req) {
        groupService.createGroup(req);
    }

    @PostMapping("/group/update")
    public void updateGroup(@RequestBody SecurityGroupUpdateReq req) {
        groupService.updateGroup(req);
    }

    /**
     * 将级联删除对应规则,并更新缓存
     *
     * @param groupId 安全组Id
     */
    @PostMapping("/group/delete")
    public void deleteGroup(@RequestBody SecurityGroupDeleteReq groupId) {
        ParamCheckUtil.checkNotNull(groupId.getGroupId(), "groupId");

        groupService.deleteGroup(groupId.getGroupId());
    }

    @PostMapping("/group/update/enable-status")
    public SecurityGroupUpdateEnableStatueRes updateGroupEnableStatueReq(@RequestBody SecurityGroupUpdateEnableStatueReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");
        ParamCheckUtil.checkNotNull(req.getEnable(), "enable");

        return groupService.updateGroupEnableStatueReq(req);
    }

    @GetMapping("/rule/page")
    public PageInfo<SecurityRuleListRes> rulePage(PageQuery pageQuery, SecurityRuleListReq req) {
        ParamCheckUtil.checkNotNull(pageQuery, "pageQuery");

        return groupService.rulePage(pageQuery, req);
    }

    @GetMapping("/rule/list")
    public List<SecurityRuleListRes> getRuleListByGroupId(SecurityRuleListReq req) {
        return groupService.ruleList(req);
    }

    @PostMapping("/rule/create")
    public void createRule(@RequestBody SecurityRuleCreateReq req) {
        groupService.createRule(req);
    }

    @PostMapping("/rule/update")
    public void updateRule(@RequestBody SecurityRuleUpdateReq req) {
        groupService.updateRule(req);
    }

    @PostMapping("/rule/delete")
    public void deleteRule(@RequestBody SecurityRuleDeleteReq ruleId) {
        groupService.deleteRule(ruleId.getRuleId());
    }


    @PostMapping("/rule/update/enable-status")
    public SecurityRuleUpdateEnableStatueRes updateRuleEnableStatueReq(@RequestBody SecurityRuleUpdateEnableStatueReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");
        ParamCheckUtil.checkNotNull(req.getEnable(), "enable");

        return groupService.updateRuleEnableStatueReq(req);
    }

}
